/*******************************************************************************
** 文件名称：global_variable.c
** 文件作用：全局变量
** 编写作者：Tom Free 付瑞彪
** 编写时间：2021-05-29
** 文件备注：
** 更新记录：
**           2021-05-29 -> 创建文件
**                                                            <Tom Free 付瑞彪>
**
**           Copyright (c) 2018-2021 付瑞彪 All Rights Reserved
**
**       1 Tab == 4 Spaces     UTF-8     ANSI C Language(C99)
*******************************************************************************/

#include "global_variable.h"
#include "eeprom.h"

/* 全局变量 */
volatile global_variable_t global_variable;
/* EEPROM变量 */
__no_init __root __eeprom eeprom_variable_t eeprom_variable @ 0x0000;

/* 初始化全局变量 */
void global_variable_init(void)
{
    global_variable.timer0_int_flag = false;
}

/* 计算EEPROM校验和 */
static uint8_t eeprom_variable_calc_checksum(void)
{
    uint8_t sum = 0;

    sum += eeprom_variable.bg_brightness;
    sum += (uint8_t)eeprom_variable.alarm_status;
    sum += eeprom_variable.alarm_time.hour;
    sum += eeprom_variable.alarm_time.minute;
    sum += eeprom_variable.alarm_time.second;

    return sum;
}

/* 初始化EEPROM变量 */
void eeprom_variable_init(void)
{
    if ((eeprom_variable.head != EEPROM_TAG_HEAD) ||
        (eeprom_variable.tail != EEPROM_TAG_TAIL) ||
        (eeprom_variable.bg_brightness < 1) ||
        (eeprom_variable.bg_brightness > 11) ||
        (eeprom_variable_calc_checksum() != eeprom_variable.checksum))
    {
        eeprom_write((uint16_t)&eeprom_variable.head, EEPROM_TAG_HEAD);
        eeprom_write((uint16_t)&eeprom_variable.tail, EEPROM_TAG_TAIL);
        eeprom_write((uint16_t)&eeprom_variable.bg_brightness, 1);
        eeprom_write((uint16_t)&eeprom_variable.alarm_status, (uint8_t)false);
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.hour, 0);
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.minute, 0);
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.second, 0);
        eeprom_write((uint16_t)&eeprom_variable.checksum, eeprom_variable_calc_checksum());
    }
}

/* 设置背景LED亮度 */
void eeprom_variable_set_bg_brightness(uint8_t value)
{
    if (value < 1)
    {
        value = 1;
    }
    else if (value > 11)
    {
        value = 11;
    }
    if (eeprom_variable.bg_brightness != value)
    {
        eeprom_write((uint16_t)&eeprom_variable.bg_brightness, value);
        eeprom_write((uint16_t)&eeprom_variable.checksum, eeprom_variable_calc_checksum());
    }
}

/* 设置闹钟开关 */
void eeprom_variable_set_alarm_status(bool status)
{
    if (eeprom_variable.alarm_status != status)
    {
        eeprom_write((uint16_t)&eeprom_variable.alarm_status, status);
        eeprom_write((uint16_t)&eeprom_variable.checksum, eeprom_variable_calc_checksum());
    }
}

/* 设置闹钟时间 */
void eeprom_variable_set_alarm_time(gm_time_t *ptime)
{
    bool modify = false;

    if (eeprom_variable.alarm_time.hour != ptime->hour)
    {
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.hour, ptime->hour);
        modify = true;
    }
    if (eeprom_variable.alarm_time.minute != ptime->minute)
    {
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.minute, ptime->minute);
        modify = true;
    }
    if (eeprom_variable.alarm_time.second != ptime->second)
    {
        eeprom_write((uint16_t)&eeprom_variable.alarm_time.second, ptime->second);
        modify = true;
    }
    if (modify)
    {
        eeprom_write((uint16_t)&eeprom_variable.checksum, eeprom_variable_calc_checksum());
    }
}
